{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "| [03_data_science/01_Numpy数组.ipynb](https://github.com/shibing624/python-tutorial/blob/master/03_data_science/01_Numpy数组.ipynb)  | Numpy array数组  |[Open In Colab](https://colab.research.google.com/github/shibing624/python-tutorial/blob/master/03_data_science/01_Numpy数组.ipynb) |\n",
    "\n",
    "# Numpy数组\n",
    "\n",
    "\n",
    "## 数组：array\n",
    "\n",
    "\n",
    "很多其他科学计算的第三方库都是以Numpy为基础建立的。\n",
    "\n",
    "Numpy的一个重要特性是它的数组计算。\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "使用前一定要先导入 Numpy 包，导入的方法有以下几种：\n",
    "```\n",
    "import numpy\n",
    "import numpy as np\n",
    "from numpy import *\n",
    "from numpy import array, sin\n",
    "```\n",
    "\n",
    "导入numpy，最常用为这种:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "假如我们想将列表中的每个元素增加1，但列表不支持这样的操作（报错）："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1, 2]\n",
    "\n",
    "# a + 1 # 报错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用numpy.array："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array(a)\n",
    "a  # [1 2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a + 1\n",
    "b  # array([2,3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与另一个 array 相加，得到对应元素相加的结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 5]\n",
      "[2 6]\n",
      "[1 8]\n"
     ]
    }
   ],
   "source": [
    "c = a + b\n",
    "print(c)  # array([3,5])\n",
    "\n",
    "# 对应元素相乘：\n",
    "print(a * b)  # [2 6]\n",
    "\n",
    "# 对应元素乘方：\n",
    "print(a ** b)  # [1 8]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 提取数组中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "[1 2]\n",
      "[3 4]\n",
      "[4 6]\n"
     ]
    }
   ],
   "source": [
    "# 提取第一个\n",
    "a = np.array([1, 2, 3, 4])\n",
    "print(a[0])  # 1\n",
    "\n",
    "# 提取前两个元素：\n",
    "print(a[:2])  # [1 2]\n",
    "\n",
    "# 最后两个元素\n",
    "print(a[-2:])  # [3 4]\n",
    "\n",
    "# 相加：\n",
    "print(a[:2] + a[-2:])  # [4 6]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 修改数组形状\n",
    "\n",
    "查看array的形状："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a.shape\n",
    "b  # (4,)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改 array 的形状：\n",
    "a.shape = 2, 2\n",
    "a\n",
    "# [[1 2]\n",
    "# [3 4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 4],\n",
       "       [6, 8]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 多维数组\n",
    "# a 现在变成了一个二维的数组，可以进行加法：\n",
    "a + a\n",
    "# [[2 4]\n",
    "#  [6 8]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  4],\n",
       "       [ 9, 16]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 乘法仍然是对应元素的乘积，并不是按照矩阵乘法来计算：\n",
    "a * a\n",
    "# [[ 1  4]\n",
    "# [ 9 16]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本节完。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}